package org.harsh.transmission.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.harsh.transmission.service.ITransMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.harsh.transmission.domain.TransTask;
import org.harsh.transmission.service.ITransTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 传输任务Controller
 * 
 * @author liuhaibo
 * @date 2024-08-25
 */
@Controller
@RequestMapping("/transmission/task")
public class TransTaskController extends BaseController
{
    private String prefix = "transmission/task";

    @Autowired
    private ITransTaskService transTaskService;
    @Autowired
    private ITransMsgService transMsgService;

    @RequiresPermissions("transmission:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询传输任务列表
     */
    @RequiresPermissions("transmission:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TransTask transTask)
    {
        startPage();
        List<TransTask> list = transTaskService.selectTransTaskList(transTask);
        for (TransTask task : list) {
            task.getTaskPriority();
        }
        return getDataTable(list);
    }

    /**
     * 导出传输任务列表
     */
    @RequiresPermissions("transmission:task:export")
    @Log(title = "传输任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TransTask transTask)
    {
        List<TransTask> list = transTaskService.selectTransTaskList(transTask);
        ExcelUtil<TransTask> util = new ExcelUtil<TransTask>(TransTask.class);
        return util.exportExcel(list, "传输任务数据");
    }

    /**
     * 新增传输任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存传输任务
     */
    @RequiresPermissions("transmission:task:add")
    @Log(title = "传输任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TransTask transTask)
    {
        return toAjax(transTaskService.insertTransTask(transTask));
    }

    /**
     * 修改传输任务
     */
    @RequiresPermissions("transmission:task:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        TransTask transTask = transTaskService.selectTransTaskByTaskId(taskId);
        mmap.put("transTask", transTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存传输任务
     */
    @RequiresPermissions("transmission:task:edit")
    @Log(title = "传输任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TransTask transTask)
    {
        return toAjax(transTaskService.updateTransTask(transTask));
    }

    /**
     * 删除传输任务
     */
    @RequiresPermissions("transmission:task:remove")
    @Log(title = "传输任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(transTaskService.deleteTransTaskByTaskIds(ids));
    }
}
